package com.example.application.views.store;
import java.beans.JavaBean;
import java.time.LocalDate;
import com.example.application.bean.Store;

import com.example.application.bean.*;
import com.example.application.views.main.MainView;
import com.example.application.views.util.DialogUtil;
import com.example.application.views.util.MyHttpClientUtil;
import com.example.application.views.util.ShowAllInfosUtil;
import com.example.page.PageComponent;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vaadin.flow.component.Text;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.dialog.Dialog;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.router.Route;

@Route(value = "supGoods", layout = MainView.class)
public class SupGoodsView extends Div {
    private Grid<SupGoods> grid;

    private User user;

    private Store store;

    private PageBean pageBean;

    public SupGoodsView(){
        user = (User) UI.getCurrent().getSession().getAttribute("user");
        store = (Store) UI.getCurrent().getSession().getAttribute("store");

        if (user == null){

        }else if(store == null){
            Dialog dialog = new Dialog();
            dialog.setCloseOnEsc(false);
            dialog.setCloseOnOutsideClick(false);
            dialog.removeAll();
            dialog.add(new Text("请先选择门店"));

            Button okBtn = new Button("去选择门店", sure ->{
//           跳转到门店管理界面
                UI.getCurrent().navigate("store");
                dialog.close();
            });

            dialog.add(new Div(okBtn, new Text("      ")));
            dialog.open();
        }else if("门店管理员".equals(user.getIdentity())){
            //在最下面
            this.init();
        }else{
            DialogUtil.adminAuthority();
        }
    }

    public void init(){
        grid = new Grid<>();
        this.pageBean = new PageBean();
        this.pageBean.setCurrentPage(1);
        this.pageBean.setPageSize(3);
        updateForm();

        grid.addColumn(SupGoods::getName).setHeader("商品名");
        grid.addColumn(SupGoods::getType).setHeader("类型");
        grid.addColumn(SupGoods::getMaterial).setHeader("材质");
        grid.addColumn(SupGoods::getApplicable).setHeader("适用规格");
        grid.addColumn(SupGoods::getExclusive).setHeader("专属规格");
        grid.addColumn(SupGoods::getPackingSize).setHeader("包装规格");
        grid.addColumn(SupGoods::getFlavor).setHeader("口味");
        grid.addColumn(SupGoods::getFunction).setHeader("特殊功用");
        grid.addColumn(SupGoods::getOrigin).setHeader("产地");
        grid.addColumn(SupGoods::getDateOfPro).setHeader("生产日期");
        grid.addColumn(SupGoods::getExpDate).setHeader("保质期");
        grid.addColumn(SupGoods::getSupId).setHeader("供货商ID");

        grid.addComponentColumn(item -> purchase(grid, item)).setHeader("操作");

        PageComponent pageComponent = new PageComponent(pageBean.getCurrentPage(), pageBean.getPageSize(), pageBean.getTolNum(), pageBean.getTolPage()) {
            @Override
            protected void pageNext() {
                SupGoodsView.this.pageBean.setCurrentPage(this.curPage);
                SupGoodsView.this.pageBean.setPageSize(this.pageSize);
                SupGoodsView.this.pageBean.setTolPage(this.tolPage);
                SupGoodsView.this.pageBean.setTolNum(this.tolNum);
                updateForm();
            }

            @Override
            protected void pagePrevious() {
                SupGoodsView.this.pageBean.setCurrentPage(this.curPage);
                SupGoodsView.this.pageBean.setPageSize(this.pageSize);
                SupGoodsView.this.pageBean.setTolPage(this.tolPage);
                SupGoodsView.this.pageBean.setTolNum(this.tolNum);
                updateForm();
            }

            @Override
            protected void gotoPage() {
                SupGoodsView.this.pageBean.setCurrentPage(this.curPage);
                SupGoodsView.this.pageBean.setPageSize(this.pageSize);
                SupGoodsView.this.pageBean.setTolPage(this.tolPage);
                SupGoodsView.this.pageBean.setTolNum(this.tolNum);
                updateForm();
            }

            @Override
            protected void firstPage() {
                SupGoodsView.this.pageBean.setCurrentPage(this.curPage);
                SupGoodsView.this.pageBean.setPageSize(this.pageSize);
                SupGoodsView.this.pageBean.setTolPage(this.tolPage);
                SupGoodsView.this.pageBean.setTolNum(this.tolNum);
                updateForm();
            }

            @Override
            protected void lastPage() {
                SupGoodsView.this.pageBean.setCurrentPage(this.curPage);
                SupGoodsView.this.pageBean.setPageSize(this.pageSize);
                SupGoodsView.this.pageBean.setTolPage(this.tolPage);
                SupGoodsView.this.pageBean.setTolNum(this.tolNum);
                updateForm();
            }

            @Override
            protected void selectPage() {
                SupGoodsView.this.pageBean.setCurrentPage(this.curPage);
                SupGoodsView.this.pageBean.setPageSize(this.pageSize);
                SupGoodsView.this.pageBean.setTolPage(this.tolPage);
                SupGoodsView.this.pageBean.setTolNum(this.tolNum);
                updateForm();
            }
        };

        pageComponent.components.add(grid);
        pageComponent.init();
        this.add(pageComponent);
    }

    public void updateForm(){
        ResultBean<PageBean> resultBean = ShowAllInfosUtil.showInfos(SupGoodsURL.ALL_SUPGOODS, this.pageBean.getCurrentPage(), this.pageBean.getPageSize(), SupGoods.class);
        this.pageBean = resultBean.getT();
        grid.setItems(resultBean.getT().getDatas());
    }

    public Button purchase(Grid<SupGoods> grid, SupGoods item){
        Button button = new Button("进货", e->{
           if(item != null){
               StoGoods sg = new StoGoods();
               sg.setName(item.getName());
               sg.setType(item.getType());
               sg.setMaterial(item.getMaterial());
               sg.setApplicable(item.getApplicable());
               sg.setExclusive(item.getExclusive());
               sg.setPackingSize(item.getPackingSize());
               sg.setFlavor(item.getFlavor());
               sg.setFunction(item.getFunction());
               sg.setOrigin(item.getOrigin());
               sg.setDateOfPro(item.getDateOfPro());
               sg.setExpDate(item.getExpDate());
               sg.setSupId(item.getSupId());
               sg.setExplain(item.getExplain());
               sg.setPrice(item.getPrice());
               sg.setPic(item.getPic());
               sg.setStore(store);

               try {
                   String msg = MyHttpClientUtil.httpPost(StoGoodsUrl.STOGOODS_ADD, sg, "utf-8");
                   ObjectMapper mapper = new ObjectMapper();
                   JavaType javaType = mapper.getTypeFactory().constructParametricType(ResultBean.class, StoGoods.class);
                   ResultBean resultBean = mapper.readValue(msg, javaType);

                   if(resultBean.getCode() == 0){
                       UI.getCurrent().getPage().executeJs("alert('操作失败');");
                   }else{
                       UI.getCurrent().getPage().executeJs("alert('操作成功');");
                   }
               } catch (JsonProcessingException jsonProcessingException) {
                   jsonProcessingException.printStackTrace();
               }
           }

        });

        return button;
    }
}
